/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using System.Threading.Tasks;
using System;

namespace NetProtocol.Server {
	/// <summary>
	/// 中心服聊天模块
	/// module : centerChat
	/// </summary>
	public interface SNetCenterChat : INetServerModule {
		Dictionary<int, MessageHandler> INetServerModule.GetModuleFunction()
        {	
            return new()
            {
                { NetMessageId.centerChat_sendChat, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.centerChat_sendChat, "Recv->" + data.ToString());
#endif
		                SendChatRequest _data = (SendChatRequest)data;
						return CS_sendChat(session, _data.chat);
		            } 
            	},
                { NetMessageId.centerChat_switchRoom, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.centerChat_switchRoom, "Recv->" + data.ToString());
#endif
		                SwitchRoomRequest _data = (SwitchRoomRequest)data;
						return CS_switchRoom(session, _data.roomId);
		            } 
            	},
                { NetMessageId.centerChat_reportPlayer, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.centerChat_reportPlayer, "Recv->" + data.ToString());
#endif
		                ReportPlayerRequest _data = (ReportPlayerRequest)data;
						return CS_reportPlayer(session, _data.pid, _data.type, _data.text, _data.reason, _data.remark);
		            } 
            	},
            };
        }
        
        Dictionary<int, Func<IReceiveMessage>> INetServerModule.GetModuleRequest() 
        {
	        return new ()
	        {
	        	{NetMessageId.centerChat_sendChat, ()=> new SendChatRequest()},
	        	{NetMessageId.centerChat_switchRoom, ()=> new SwitchRoomRequest()},
	        	{NetMessageId.centerChat_reportPlayer, ()=> new ReportPlayerRequest()},
	        };
	    }
        
#region Server-side Interface
		/// <summary>
        ///  发送聊天
        /// </summary>
	    protected SendChatResult CS_sendChat(ISession session, CChatPOD chat);
		/// <summary>
        ///  切换房间
        /// </summary>
	    protected SwitchRoomResult CS_switchRoom(ISession session, int roomId);
		/// <summary>
        /// 举报玩家(type:聊天类型(聊天频道,比如世界,私聊) , text:举报内容(被举报人发言内容) reason:举报原因(举报分类名称，如：发布广告。多个用英文分号分割), remark:举报人备注内容)
        /// </summary>
	    protected ReportPlayerResult CS_reportPlayer(ISession session, long pid, int type, string text, string reason, string remark);
#endregion
#region Client-side Interface
		/// <summary>
        /// 
        /// </summary>
        public SendChatResult SC_sendChatResult(Task<SendChatResult> future){return new SendChatResult(future);}
        public SendChatResult SC_sendChatResult(int code){return new SendChatResult(code);}
		/// <summary>
        /// 
        /// </summary>
        public SwitchRoomResult SC_switchRoomResult(Task<SwitchRoomResult> future){return new SwitchRoomResult(future);}
        public SwitchRoomResult SC_switchRoomResult(int code, ChatRoomPOD chatRoom){return new SwitchRoomResult(code, chatRoom);}
        public SwitchRoomResult SC_switchRoomResult(int code){return new SwitchRoomResult(code);}
		/// <summary>
        ///  通知聊天信息
        /// </summary>
		public bool SC_notifyChat(ISession session, SChatPOD chat){
	        return NetProtocolHelper.SendMessage(session, new NotifyChat(chat));
	    }
	    public void SC_notifyChat_Broadcast(List<ISession> sessions, SChatPOD chat){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifyChat(chat));
	    }
		/// <summary>
        ///  通知聊天室更新
        /// </summary>
		public bool SC_notifyChatRoom(ISession session, ChatRoomPOD chatRoom){
	        return NetProtocolHelper.SendMessage(session, new NotifyChatRoom(chatRoom));
	    }
	    public void SC_notifyChatRoom_Broadcast(List<ISession> sessions, ChatRoomPOD chatRoom){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifyChatRoom(chatRoom));
	    }
		/// <summary>
        /// 
        /// </summary>
        public ReportPlayerResult SC_reportPlayerResult(Task<ReportPlayerResult> future){return new ReportPlayerResult(future);}
        public ReportPlayerResult SC_reportPlayerResult(int code){return new ReportPlayerResult(code);}
#endregion

#region Messages
	public class SendChatResult : MessageResult {
	    public SendChatResult(Task<SendChatResult> future): base(future) { }
		public SendChatResult(int code): base(code) { }
		public new SendChatResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
        public override MessageResult GetFutureResult(Task task) { return ((Task<SendChatResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.centerChat_sendChatResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.centerChat_sendChatResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			return si_p;
		}
		public override string ToString() {
    		return "CenterChat.SC_sendChatResult { "
			+ "code="
			+ code
			+" }";
		}
	}
	public class SwitchRoomResult : MessageResult {
	    private ChatRoomPOD chatRoom;
	    public SwitchRoomResult(Task<SwitchRoomResult> future): base(future) { }
		public SwitchRoomResult(int code): base(code) { }
		public new SwitchRoomResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
	    internal SwitchRoomResult(int code, ChatRoomPOD chatRoom){
	        this.code = code;
	        this.chatRoom = chatRoom;
	    }
        public override MessageResult GetFutureResult(Task task) { return ((Task<SwitchRoomResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.centerChat_switchRoomResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.centerChat_switchRoomResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			_out.Write_pod(chatRoom);
			return si_p;
		}
		public override string ToString() {
    		return "CenterChat.SC_switchRoomResult { "
			+ "code="
			+ code
			+ ", chatRoom="
			+ chatRoom
			+" }";
		}
	}
	public class NotifyChat : MessageNotify {
		private SChatPOD chat;
		public NotifyChat(SChatPOD chat) {
		    this.chat = chat;
		}
		public override int GetMessageId() {return NetMessageId.centerChat_notifyChat;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.centerChat_notifyChat;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_pod(chat);
			return si_p;
		}
		public override string ToString() {
			return "CenterChat.SC_notifyChat { "
			+ "chat="
			+ chat
			+" }";
		}
	}
	public class NotifyChatRoom : MessageNotify {
		private ChatRoomPOD chatRoom;
		public NotifyChatRoom(ChatRoomPOD chatRoom) {
		    this.chatRoom = chatRoom;
		}
		public override int GetMessageId() {return NetMessageId.centerChat_notifyChatRoom;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.centerChat_notifyChatRoom;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_pod(chatRoom);
			return si_p;
		}
		public override string ToString() {
			return "CenterChat.SC_notifyChatRoom { "
			+ "chatRoom="
			+ chatRoom
			+" }";
		}
	}
	public class ReportPlayerResult : MessageResult {
	    public ReportPlayerResult(Task<ReportPlayerResult> future): base(future) { }
		public ReportPlayerResult(int code): base(code) { }
		public new ReportPlayerResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
        public override MessageResult GetFutureResult(Task task) { return ((Task<ReportPlayerResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.centerChat_reportPlayerResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.centerChat_reportPlayerResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			return si_p;
		}
		public override string ToString() {
    		return "CenterChat.SC_reportPlayerResult { "
			+ "code="
			+ code
			+" }";
		}
	}
	private class SendChatRequest : IReceiveMessage
    {
		internal CChatPOD chat;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            chat = _in.HasRemaining() ? _in.Read_pod<CChatPOD>(CChatPOD.DynamicFactory) : default;
		}
        public int GetMessageId() { return NetMessageId.centerChat_sendChat; }
        public override string ToString()
        {
            return "CenterChat.sendChat"+ ", chat="+ chat;
        }
    }
	private class SwitchRoomRequest : IReceiveMessage
    {
		internal int roomId;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            roomId = _in.HasRemaining() ? _in.Read_int() : default;
		}
        public int GetMessageId() { return NetMessageId.centerChat_switchRoom; }
        public override string ToString()
        {
            return "CenterChat.switchRoom"+ ", roomId="+ roomId;
        }
    }
	private class ReportPlayerRequest : IReceiveMessage
    {
		internal long pid;
		internal int type;
		internal string text;
		internal string reason;
		internal string remark;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            pid = _in.HasRemaining() ? _in.Read_long() : default;
            type = _in.HasRemaining() ? _in.Read_int() : default;
            text = _in.HasRemaining() ? _in.Read_string() : default;
            reason = _in.HasRemaining() ? _in.Read_string() : default;
            remark = _in.HasRemaining() ? _in.Read_string() : default;
		}
        public int GetMessageId() { return NetMessageId.centerChat_reportPlayer; }
        public override string ToString()
        {
            return "CenterChat.reportPlayer"+ ", pid="+ pid+ ", type="+ type+ ", text="+ text+ ", reason="+ reason+ ", remark="+ remark;
        }
    }
	}
#endregion
}